package cn.jcet.mapreduce.nline;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.NLineInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * @author 鲜磊
 * @Date 2020/12/7 23:24
 */
public class NLineDriver {

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        args = new String[]{"d:/code/lineinput","e:/code/lineoutput"};

        Configuration conf = new Configuration();
        // 1、获取job对象
        Job job = Job.getInstance(conf);
        // 设置任务的切片数划分记录数
        NLineInputFormat.setNumLinesPerSplit(job,3);
        // 使用NLineInputFormat处理记录数
        job.setInputFormatClass(NLineInputFormat.class);
        //2、设置jar位置
        job.setJarByClass(NLineDriver.class);
        //3、关联mapper和reduce位置
        job.setMapperClass(NLineMapper.class);
        job.setReducerClass(NLineReduce.class);
        //4、map的数据数据类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        //5、最终输出类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        //6、设置输出输出路径
        FileInputFormat.setInputPaths(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));
        //7、提交任务
        boolean result = job.waitForCompletion(true);
        System.exit(result?0:1);
    }
}
